﻿using Error_Handler_Control;
using PollSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PollSystem.Registered
{
    public partial class ViewAllResults : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.LiteralQuestion.Text = "";
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<Question> GridViewQuestions_GetData()
        {
            PollSystemEntities context = new PollSystemEntities();
            return context.Questions.OrderBy(q => q.QuestionText);
        }

        protected void GridViewQuestions_SelectedIndexChanged(object sender, EventArgs e)
        {
            PollSystemEntities context = new PollSystemEntities();
            using (context)
            {
                var questionId = Convert.ToInt32(this.GridViewQuestions.SelectedDataKey.Value);

                Question question = context.Questions
                                                .Include("Answers")
                                                .FirstOrDefault(q => q.QuestionId == questionId);

                this.LiteralQuestion.Text = question.QuestionText;

                this.ChartVoteResults.DataSource = question.Answers.ToList();
                this.ChartVoteResults.Series["SeriesVotes"].XValueMember = "AnswerText";
                this.ChartVoteResults.Series["SeriesVotes"].YValueMembers = "Votes";
                this.ChartVoteResults.Series["SeriesVotes"].Label = "#PERCENT";
                this.ChartVoteResults.Series["SeriesVotes"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
                this.DataBind();
            }
        }

        protected void ButtonCreateQuestion_Click(object sender, EventArgs e)
        {
            this.FormViewCreateQuestion.Visible = true;
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            RequiredFieldValidator validator = (RequiredFieldValidator)this.FormViewCreateQuestion.FindControl("RequiredFieldValidatorEmptyBox");
            this.FormViewCreateQuestion.Visible = false;
            Response.Redirect("ViewAllResults.aspx");
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            TextBox questionBox = (TextBox)this.FormViewCreateQuestion.FindControl("TextBoxQuestion");
            PollSystemEntities context = new PollSystemEntities();

            using (context)
            {
                if (!string.IsNullOrEmpty(questionBox.Text))
                {
                    try
                    {
                        Question newQuestion = new Question()
                        {
                            QuestionText = questionBox.Text
                        };

                        context.Questions.Add(newQuestion);
                        context.SaveChanges();
                        this.FormViewCreateQuestion.Visible = false;
                        ErrorSuccessNotifier.ShowAfterRedirect = true;
                        ErrorSuccessNotifier.AddSuccessMessage("Question created.");
                    }
                    catch (Exception ex)
                    {
                        ErrorSuccessNotifier.AddErrorMessage(ex);
                        return;
                    }
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("Please enter question or click cancel!");
                }
            }
        }
    }
}